﻿using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
using Unity.Collections;

[UpdateInGroup(typeof(SimulationSystemGroup), OrderFirst = true)]
partial struct ResetTargetSystem : ISystem
{
    private ComponentLookup<LocalTransform> localTransformComponentLookup;
    private EntityStorageInfoLookup entityStoreageInfoLookup;
    public void OnCreate(ref SystemState state)
    {
        localTransformComponentLookup = state.GetComponentLookup<LocalTransform>(true);
        entityStoreageInfoLookup = state.GetEntityStorageInfoLookup();
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        localTransformComponentLookup.Update(ref state);
        entityStoreageInfoLookup.Update(ref state);
        ResetTargetJob resetTargetJob = new ResetTargetJob
        {
            localTransformComponentLookup = localTransformComponentLookup,
            entityStoreageInfoLookup = entityStoreageInfoLookup,
        };
        resetTargetJob.ScheduleParallel();

        //foreach (RefRW<Target> target in SystemAPI.Query<RefRW<Target>>())
        //{
        //    if (target.ValueRW.tagertEntity != Entity.Null)
        //    {
        //        if (SystemAPI.Exists(target.ValueRO.tagertEntity) == false || SystemAPI.HasComponent<LocalTransform>(target.ValueRO.tagertEntity) == false)
        //        {
        //            UnityEngine.Debug.Log("Rest");
        //            target.ValueRW.tagertEntity = Entity.Null;
        //        }
        //    }
        //}

        TargetOverrideJob targetOverrideJob = new TargetOverrideJob
        {
            localTransformComponentLookup = localTransformComponentLookup,
            entityStoreageInfoLookup = entityStoreageInfoLookup,
        };
        targetOverrideJob.ScheduleParallel();

        //foreach (RefRW<TargetOverride> targetOverride in SystemAPI.Query<RefRW<TargetOverride>>())
        //{
        //    if (targetOverride.ValueRW.tagertEntity != Entity.Null)
        //    {
        //        if (SystemAPI.Exists(targetOverride.ValueRO.tagertEntity) == false || SystemAPI.HasComponent<LocalTransform>(targetOverride.ValueRO.tagertEntity) == false)
        //        {
        //            UnityEngine.Debug.Log("Rest");
        //            targetOverride.ValueRW.tagertEntity = Entity.Null;
        //        }
        //    }
        //}
    }

}

[BurstCompile]
public partial struct ResetTargetJob : IJobEntity
{
    [ReadOnly] public ComponentLookup<LocalTransform> localTransformComponentLookup;
    [ReadOnly] public EntityStorageInfoLookup entityStoreageInfoLookup;

    public void Execute(ref Target target)
    {
        if (target.tagertEntity != Entity.Null)
        {
            if (entityStoreageInfoLookup.Exists(target.tagertEntity) == false || localTransformComponentLookup.HasComponent(target.tagertEntity) == false)
            {
                //UnityEngine.Debug.Log("Rest");
                target.tagertEntity = Entity.Null;
            }
        }
    }
}

[BurstCompile]
public partial struct TargetOverrideJob : IJobEntity
{
    [ReadOnly] public ComponentLookup<LocalTransform> localTransformComponentLookup;
    [ReadOnly] public EntityStorageInfoLookup entityStoreageInfoLookup;

    public void Execute(ref TargetOverride targetOverride)
    {
        if (targetOverride.tagertEntity != Entity.Null)
        {
            if (entityStoreageInfoLookup.Exists(targetOverride.tagertEntity) == false || localTransformComponentLookup.HasComponent(targetOverride.tagertEntity) == false)
            {
                //UnityEngine.Debug.Log("Rest");
                targetOverride.tagertEntity = Entity.Null;
            }
        }
    }
}